class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>> edges(numCourses);
        vector<int> in(numCourses);
        for (auto& e : prerequisites)
        {
            int a = e[0], b = e[1];
            edges[b].push_back(a);
            in[a]++;
        }
        queue<int> q;
        for (int i = 0; i < numCourses; i++)
        {
            if (in[i] == 0)
                q.push(i);
        }
        vector<int> ret;
        while (q.size())
        {
            int t = q.front();
            q.pop();
            ret.push_back(t);
            for (auto e : edges[t])
            {
                in[e]--;
                if (in[e] == 0)
                {
                    q.push(e);
                }
            }
        }
        if (ret.size() == numCourses) return ret;
        else return {};

    }
};